//
//  RHStatusViewModel.swift
//  微博新浪
//
//  Created by hezerong on 16/6/14.
//  Copyright © 2016年 Rocky. All rights reserved.
//

import UIKit

//图片视图布局结构体

struct  cellLayout {
    
    //视图间隔
    let separator :CGFloat = 12
    //图片视图宽度
    let PictureViewWidth :CGFloat
    //单张图片宽度
    let singlePictureWidth :CGFloat
    
    init(){
    
        PictureViewWidth = screenWidth - separator * 2
        singlePictureWidth = (PictureViewWidth - separator * 2 )/3
    }
}

//定义一个布局机构提常量
let cellLayout_constant = cellLayout()

class RHStatusViewModel: NSObject {
    
    var statusModel:RHStatusModel
    
    //会员等级图片
    var levelIconImage:UIImage?
    //vip认证图片
    var vipIconImage:UIImage?
    //来源文字
    var sourceText:String?
    //时间字符串
    var timeTexst:String?
    ///转发微博的配图视图的大小
    var retweetedPictureViewSize: CGSize = CGSizeZero
    //单张图片大小
    var singlePictureViewSize:CGSize = CGSizeZero
    //转发微博文字
    var retweetedTexst:String?
    //图片宽度
    var pictureViewSize:CGSize = CGSizeZero
    //转发微博文字信息
    var retweetedText:String?
    //配图数据数组
    var pic_urls:[RHPictureModel]?{
    
        //如果有原创图片，则将原创的picture赋值给pic_urls
        if let pictures = statusModel.pic_urls where pictures.count > 0{
            return pictures
        }
        //如果转发微博有图片，则将原创微博的picture赋值给pic_urls
        if let pictures = statusModel.retweeted_status?.pic_urls where pictures.count > 0 {
            return pictures
        }
        
        return nil
    }

    
    init(statusModel:RHStatusModel) {
        
        self.statusModel = statusModel
        super.init()
        
        createUserData()
        
        //处理微博时间和来源数据
        createStatusData()
        
        createRetweetedData()
        //计算原创微博配图视图size
        pictureViewSize = calculatePictureViewSize(statusModel.pic_urls)
    }
    

    //处理转发微博数据
    func createRetweetedData(){
    
        guard let retweeted_status = statusModel.retweeted_status else{
           
            return
        }
        //处理转发微博文字
        retweetedText = "@\((retweeted_status.user?.screen_name)!):\(retweeted_status.text!)"
        
        
    }
    
    func createUserData(){
    
        if statusModel.user?.mbrank > 0 && statusModel.user?.mbrank < 7{
        
            if let mbrank = statusModel.user?.mbrank{
            
                levelIconImage = UIImage(named: "common_icon_membership_level\(mbrank)")

            }
        
        }
        
        //处理认证类型图像
        //认证类型：-1：没有认证，0：认证用户，2，3，5：企业认证， 220：达人
        if let verified_type = statusModel.user?.verified_type{
        
            switch verified_type {
               
            case 0:
                self.vipIconImage = UIImage(named: "avatar_vip")
                
            case 2,3,5:
                self.vipIconImage = UIImage(named: "avatar_enterprise_vip")
                
            case 220:
                self.vipIconImage = UIImage(named: "avatar_grassroot")
                
            default:break
            
            }
        }
    }
    
    //计算图片视图的size
    func calculatePictureViewSize(pictureModel:[RHPictureModel]?)->CGSize{
    
        if let count = statusModel.pic_urls?.count where count > 0{
        
           //分页算法 , 计算当前多少行
            let row = (count - 1)/3 + 1
            //根据行数和当行的高度，算出图片视图的高度
            let pictureViewHeight = cellLayout_constant.singlePictureWidth * CGFloat(row) + CGFloat(row-1) * cellLayout_constant.separator
            
            return CGSizeMake(cellLayout_constant.singlePictureWidth, pictureViewHeight)
        }else
        {
        
            //没有图片
            return CGSizeZero
        
        }
    }
    
    
    //处理微博信息数据
    func createStatusData(){
    
        if let sources = statusModel.source{
        
//            print(statusModel.source)
           let startIndex = sources.rangeOfString("\">")?.endIndex
           let endIndex = sources.rangeOfString("</a>")?.startIndex
            
            if let startindex = startIndex,endindex = endIndex{
                
                sourceText = "frome \(sources.substringWithRange(startindex..<endindex))"
               
            }
        }
        
        //处理时间字符串
        let sinaDate = NSDate.getDateFromSina(statusModel.created_at!)
        timeTexst = sinaDate.sina_description
    }
    
  
}
